package com.kaishengit.service;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.kaishengit.dao.EmailDao;
import com.kaishengit.pojo.Company;
import com.kaishengit.pojo.Contact;
import com.kaishengit.pojo.Email;
import com.kaishengit.util.DateUtil;

@Transactional
public class EmailService {
	
	private EmailDao emailDao;
	
	public void savemail(String[] email, String[] emailtype, Contact contact) {
		emailDao.delAll(findByObject(contact));
		if(email != null && emailtype != null) {
			for(int i = 0; i < email.length; i ++) {
				for(int j = 0; j < emailtype.length; j ++) {
					if(i == j) {
						Email email2 = new Email();
						email2.setEmail(email[i]);
						email2.setType(emailtype[j]);
						email2.setCreatetime(DateUtil.getNow());
						email2.setContact(contact);
						emailDao.save(email2);
					}
				}
			}
			
		}
	}

	
	
	public EmailDao getEmailDao() {
		return emailDao;
	}

	public void setEmailDao(EmailDao emailDao) {
		this.emailDao = emailDao;
	}



	public void savemail(String[] email, String[] emailtype, Company company) {
		if(email != null && emailtype != null) {
			for(int i = 0; i < email.length; i ++) {
				for(int j = 0; j < emailtype.length; j ++) {
					if(i == j) {
						Email email2 = new Email();
						email2.setEmail(email[i]);
						email2.setType(emailtype[j]);
						email2.setCreatetime(DateUtil.getNow());
						email2.setCompany(company);
						emailDao.save(email2);
					}
				}
			}
			
		}
		
	}



	public List<Email> findByObject(Contact contact) {
			String hql = "select h from Email as h where h.contact.id = ?";
			return emailDao.find(hql, contact.getId());
		}



	public void delmail(Contact contact) {
		emailDao.delAll(findByObject(contact));
	}
	
	

}
